package org.bpms.service.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.bpms.model.admin.Book;
import org.bpms.model.admin.Booktemp;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 临时档案操作接口
 * @author hupei
 *
 */
public class BooktempService {
	
	/**
	 * 保存 
	 */
	@Before(Tx.class)
	public void save(Booktemp temp){
		if (null == temp.get("id")){
			temp.set("id", UUID.randomUUID().toString().replaceAll("-", ""));
			temp.save();
		}
	}
	
	/**
	 * 清空历史表
	 */
	@Before(Tx.class)
	public void deleteAll(){
		Db.update("delete from booktemp");
	}
	
	/**
	 * 获取统一tempid的记录
	 * @param random
	 * @param uid
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findTemp(int random , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer(" from booktemp where 1=1 and tempid="+random);
		sql.append(" order by "+orderField+" "+orderDirection);
		Page<Booktemp> ps = Booktemp.me.paginate(pageNumber, pageSize, "select * ", sql.toString());
		pagemap.put("pageSize", ps.getPageSize());//页面大小
		pagemap.put("totalRow", ps.getTotalRow());//总行数
		pagemap.put("pageCurrent", ps.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", ps.getList());//页面数据
	}
	
	/**
	 * 分页获取需要覆盖的数据
	 * @param random
	 * @param uid
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findTempToPut(int random , List<Booktemp> lb , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer(" from booktemp where 1=1 and tempid="+random);
		if (lb.size() == 1){
			sql.append(" and bookid='"+lb.get(0).getStr("bookid")+"'");
		}else{
			for (int i = 0 ; i < lb.size() ; i++){
				if (i == 0){
					sql.append(" and bookid in('"+lb.get(i).getStr("bookid")+"'");
				}else if (i == lb.size()-1){
					sql.append(",'"+lb.get(i).getStr("bookid")+"')");
				}else{
					sql.append(",'"+lb.get(i).getStr("bookid")+"'");
				}
			}			
		}
		sql.append(" order by "+orderField+" "+orderDirection);
		Page<Booktemp> ps = Booktemp.me.paginate(pageNumber, pageSize, "select * ", sql.toString());
		pagemap.put("pageSize", ps.getPageSize());//页面大小
		pagemap.put("totalRow", ps.getTotalRow());//总行数
		pagemap.put("pageCurrent", ps.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", ps.getList());//页面数据
	}
	
	/**
	 * 查找可能需要覆盖的临时档案
	 * @param random
	 * @return
	 */
	public List<Booktemp> findFromrandom(int random , String flag ,String uid){
		List<Booktemp> lb = new ArrayList<Booktemp>();
		List<Booktemp> bl = Booktemp.me.find("select * from booktemp where  tempid=?" , random);
		if (null != bl && bl.size() > 0){
			for (Booktemp temp : bl){
				String bookid = temp.getStr("bookid")==null?"":temp.getStr("bookid");
				List<Book> lbook = Book.me.find("select * from book where  state=? and bookid=?" ,  flag , bookid);
				if (null != lbook && lbook.size() > 0){
					lb.add(temp);
				}
			}
		}
		return lb;
	}
	
	/**
	 * 查找所有tempid的临时档案
	 * @param random
	 * @return
	 */
	public List<Booktemp> findAllTemp(int random){
		return Booktemp.me.find("select * from booktemp where tempid=?" , random);
	}
	
	/**
	 * 导入成功后删除临时档案
	 * @param tempid
	 */
	@Before(Tx.class)
	public void remove(int tempid){
		String sql = "delete from booktemp where tempid=?";
		Db.update(sql , tempid);
	}
}
